package net.digitalartwork.geoslideshow {
	import flash.display.*;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.TextField;
	
	import net.digitalartwork.util.String2;
	import net.digitalartwork.ui.BasicButton;
	import net.digitalartwork.ui.Tooltip;
	import net.digitalartwork.ui.ImageMakerLite;
	import net.digitalartwork.ui.SunDraw;
	
	import net.digitalartwork.geoslideshow.DASortButton;
	import net.digitalartwork.mp3player.DAMp3Player;
	
	import gs.TweenFilterLite;
	import gs.OverwriteManager;
	import gs.easing.*;
	
	import br.com.stimuli.loading.BulkLoader;
    import br.com.stimuli.loading.BulkProgressEvent;
	
	
	public class DAThumbBox extends MovieClip{
		
		//--------------------------------------------------------------------------
		// ASSETS
		//--------------------------------------------------------------------------
		public var thumb_bounds:MovieClip;
		public var thumb_holder_bounds:MovieClip;
		public var thumb_holder_mask:SunDraw;
		public var current_t:MovieClip;
		public var thumb_holder:MovieClip;
		public var bg:MovieClip;
		
		public var thumb_titleToggle:MovieClip;
		
		public var sort_button:DASortButton;
		
		public var mp3player_mc:DAMp3Player;
		//--------------------------------------------------------------------------
		// ARRAYS
		//--------------------------------------------------------------------------
		private var image_array:Array = [];
		private var image_array_sort:Array = []
		//--------------------------------------------------------------------------
		// Properties
		//--------------------------------------------------------------------------
		public var _scale:Number;
		public var _height:Number;
		public var _width:Number;
		public var _thumbwidth:Number;
		public var _thumbheight:Number;
		public var _spacing:Number = 5;
		private var _color:Number = 0x336699;
		private var _colorselect:Number = 0x89B3CF;
		
		
		private var tXML:XMLList; // list of itemXML


		public var current_number:Number = 0;
		
		public var _ready:Boolean = false;
		
		public var _sort_name:String = "edit";
		//--------------------------------------------------------------------------
		// Dispatch
		//--------------------------------------------------------------------------
		public var IMAGE_CHANGE:String = "imagechange";
		public var SORT_CHANGE:String = "sortchange";
		//--------------------------------------------------------------------------
		// BulkLoader
		//--------------------------------------------------------------------------
		public var bLoader:BulkLoader;
		
		public function DAThumbBox() {
			_sort_name
			sort_button.addEventListener(sort_button.SORT_CHANGE, onSortChange)
			trace("DATThumbBox Init");

			_scale = this.scaleX;
			this.scaleX = 1;
			this.scaleY = 1;
			thumb_holder_bounds.width = thumb_holder_bounds.width*_scale;
			bg.width = bg.width*_scale
			
			_height = thumb_bounds.height;
			_width = thumb_bounds.width;
			_thumbwidth = thumb_holder_bounds.height - 2;
			_thumbheight = thumb_holder_bounds.height - 2;
			
			thumb_bounds.mc_bounds_outline.alpha = 0;
			thumb_holder = new MovieClip();
			addChild(thumb_holder);
			addChild(current_t);
			thumb_holder.y = thumb_holder_bounds.y+1;
			thumb_holder.x = thumb_holder_bounds.x+1;
			
			//--------------------------------------------------------------------------
			// MASK
			//--------------------------------------------------------------------------
			thumb_holder_mask = new SunDraw({shapeType:"rectangle", shapeWidth:thumb_holder_bounds.width, shapeHeight:thumb_holder_bounds.height, hexFill:0x000000, shapeFillAlpha:1});
			addChild(thumb_holder_mask);
			thumb_holder.mask = thumb_holder_mask;
			thumb_holder_mask.y = thumb_holder.y;
			thumb_holder_mask.x = thumb_holder.x;
			thumb_holder.visible = false;
			
			mp3player_mc.addEventListener(mp3player_mc.ON_MP3START, onMP3Init);
			mp3player_mc.addEventListener(mp3player_mc.ON_MP3NEWAUDIO, onMP3Init);
		}
		
		private function onMP3Init(e:Event):void {
			
			TweenFilterLite.to(mp3player_mc, .5, {x:bg.width - mp3player_mc._w -25, alpha:1, ease:Linear.easeOut});
		}
		
		private function onSortChange(e:Event):void {
			_sort_name = sort_button._sort_name;
			dispatchEvent(new Event(SORT_CHANGE));
		}
		
		public function populateBox(thumbXML:XML):void {
			tXML = thumbXML.item;
			var i:int = 0;
			
			bLoader = new BulkLoader("thumbs");
			//bLoader.logLevel = BulkLoader.LOG_INFO;
			//trace(thumbXML.item[0].thumb[0])
			
			for each (var item in tXML) {
				var s:String = item.thumb[0];
				bLoader.add(s);
			}
			
			var fst:String = thumbXML.item[0].thumb[0];
			bLoader.get(fst).addEventListener(Event.COMPLETE, onBulkFirstLoad)
			
			bLoader.addEventListener(BulkLoader.COMPLETE, onBulkLoadComplete);
			bLoader.addEventListener(BulkLoader.PROGRESS, onBulkLoadProgress);
            
            // now start the loading
            bLoader.start();
		}
		
		private function onBulkFirstLoad(e:Event):void {
			trace("FIRST LOAD");
		}
		
		private function onBulkLoadProgress(e:BulkProgressEvent):void {
			//trace(e.loadingStatus());
		}
		
		private function onBulkLoadComplete(e:Event):void {
			var t:int;
			for each (var item in tXML) {
				makeItem(item, t);
				t++;
			}
			thumb_holder.visible = true;
			_ready = true;
			scrollTo(0);
		}
		
		private function makeItem(ix:XML, t:Number):void {
			
			var s:String = ix.thumb[0];

			var sort_date:Date = String2.fixDate(ix.exif[0].date[0]);
			
			var sort_object:Object = new Object();
			sort_object["_rating"] = ix.meta[0].attribute("rating");
			sort_object["_color"] = ix.meta[0].attribute("color");
			sort_object["_date"] = sort_date;
			sort_object["_edit"] = t;
			
			var info_object:Object = new Object();
			info_object["_width"] = ix.meta[0].attribute("width");
			info_object["_height"] = ix.meta[0].attribute("height");
			info_object["_media"] = ix.media[0];
			info_object["_thumb"] = ix.thumb[0];
			info_object["_number"] = t;
			
			var tmc:ImageMakerLite = new ImageMakerLite(bLoader.getBitmap(s), sort_object, _thumbwidth, _thumbheight, t);

			thumb_holder.addChild(tmc);
			image_array.push(tmc);
			image_array_sort.push(tmc);

			//tmc.makeBW(.5);
			tmc.makeAlpha(.5);
			tmc.buttonMode = true; 
			tmc.useHandCursor = true;
			tmc.addEventListener(tmc.IMAGE_ROLLOVER, onThumbRollOver);
			tmc.addEventListener(tmc.IMAGE_ROLLOUT, onThumbRollOut);
			tmc.addEventListener(tmc.IMAGE_DOWN, onThumbDown);
			
			//tmc.x = ((_thumbwidth+_spacing)*t);
			
			sortBox("edit");
			
		}
		
		public function updateDisplay():void {
			
			var i:int = 0;
			for (i = 0; i < image_array.length; i++){
				TweenFilterLite.to(image_array[i], .5, {x:((_thumbwidth+_spacing)*i), ease:Expo.easeInOut});
			}
			
			
		}
		private function onImageLoadedHandeler(e:Event):void {
			if (e.target.getNumVar() == 0) {
				scrollTo(0);
			}
		}
		private function onThumbRollOver(e:Event):void {
			//TweenFilterLite.to(e.target, .2, {alpha:1});
			
			e.target.makeAlpha(1);
		};
		private function onThumbRollOut(e:Event):void {
			if (e.target.getNumVar() == current_number) {
			} else {
				e.target.makeAlpha(.5);
			}
			
		};
		private function onThumbDown(e:Event):void {
			
			current_number = e.target.getNumVar()
			scrollTo(current_number);
			dispatchEvent(new Event(IMAGE_CHANGE));
			
		};
		public function scrollTo(n:Number, noLoad:Boolean=false):void {
			
			
			
			if (_ready) {
				current_number = n;
				var tmc = image_array[n];
				//TweenFilterLite.to(thumb_holder, 1, {x:-tmc.x+thumb_holder_bounds.x+(thumb_holder_bounds.width/2) - ((tmc.width+_spacing)/2), y:tmc.y+thumb_holder.y, ease:Expo.easeInOut});
				var tmpX:Number = -tmc.x - (tmc.width/2) + (thumb_holder_bounds.width/2);

				TweenFilterLite.to(thumb_holder, 1, {x:tmpX, y:tmc.y+thumb_holder.y, ease:Expo.easeInOut});
				//tmc.makeColor();


				for (var i:int = 0; i < image_array.length; i++){
					trace(_thumbwidth)
					if (i == current_number) {
						image_array[i].makeAlpha(1);
						TweenFilterLite.to(image_array[i], .5, {alpha:1, ease:Linear.easeNone});
					} else {

						if (image_array[i].alpha > .55) {
							image_array[i].makeAlpha(.5);
							TweenFilterLite.to(image_array[i], .5, {alpha:.7, ease:Linear.easeNone});
						}


					}

				}
			}
			
			
		}
		
		public function sortBox(mthd:String="date"):void {
			trace("-----------BEFORE THUMB");
			
			
			if (mthd == "date") {
				image_array_sort.sortOn("_date", Array.NUMERIC);
			}
			if (mthd == "edit") {
				image_array_sort.sortOn("_edit", Array.NUMERIC);
			}
			if (mthd == "color") {
				image_array_sort.sortOn("_color", Array.DESCENDING);
			}
			if (mthd == "rating") {
				image_array_sort.sortOn("_rating", Array.NUMERIC);
			}
			
			trace("-----------AFTER THUMB");
			
			var k:int=0;
			
			for (var i:int = 0; i < image_array_sort.length; i++) {
				
				if (mthd == "edit") {
					if (image_array_sort[i]._rating == "nil") {
						//image_array_sort[i].visible = false;
						TweenFilterLite.to(image_array_sort[i], 1, {scaleY:0, scaleX:0, y:0 , ease:Expo.easeOut, onComplete:hideImage, onCompleteParams:[image_array_sort[i]]});
						
					} else {
						image_array_sort[i].visible = true;
						TweenFilterLite.to(image_array_sort[i], 1, {scaleY:1, scaleX:1, y:0, x:((_thumbwidth+_spacing)*k), ease:Expo.easeInOut});
						//_imageholder_width = _imageholder_width + image_array_sort[i].width + _spacing;
						k++
					}
				} else {
					image_array_sort[i].visible = true;
					TweenFilterLite.to(image_array_sort[i], 1, {scaleY:1, scaleX:1, y:0, x:((_thumbwidth+_spacing)*k), ease:Expo.easeInOut});
					k++
					//_imageholder_width = _imageholder_width + image_array_sort[i].width + _spacing;
				}
				if (mthd == "date") {
					image_array_sort[i].viewMode("date")
				} else {
					if (mthd == "color") {
						image_array_sort[i].viewMode("color");
					} else {
						image_array_sort[i].viewMode("none");
					}
					
				}
				
			}
			
			function hideImage(remi):void {
				remi.visible = false;
			}
			/*
			for (var i:int = 0; i < image_array_sort.length; i++){
				trace(image_array_sort[i]._date);
				trace(image_array_sort[i]._edit);
				TweenFilterLite.to(image_array_sort[i], 1, {x:((_thumbwidth+_spacing)*i), ease:Expo.easeInOut});
				
				//image_array_sort[i].setNumVar(i);

			}
			*/
			TweenFilterLite.to(thumb_holder, 1, {alpha:1, ease:Expo.easeInOut, onComplete:scrollTo, onCompleteParams:[current_number]});
			
		}
		
		
		
	}
	
}